package com.llkj.qingnianguoxue.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.VideoDetailCommentAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.CommentListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 全部评论
 */
public class CommentListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private static final int REQUEST_EDIT_COMMENT_CODE = 8951;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvComment;//评论列表
    private VideoDetailCommentAdapter commentAdapter;//评论适配器
    private List<CommentListBean.CommentBean> commentData;//评论数据
    private int totalSize,mPage;
    private String mVedioId;//视频id
    private boolean haveNewComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_list);
        setTitle("评论", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_comment_edit);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle != null){
            mVedioId = bundle.getString("id");
        }

        initView();
        initListener();

        onRefresh();
    }

    private void initView(){
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvComment = (RecyclerView) findViewById(R.id.recyclerView);
        int padding = DisplayUtil.dip2px(this, 10);
        lvComment.setPadding(padding, 0, padding, padding);
        lvComment.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        commentData = new ArrayList<>();
        commentAdapter = new VideoDetailCommentAdapter(R.layout.item_video_detail_comment, commentData);
        lvComment.setAdapter(commentAdapter);
    }

    private void initListener(){
        refreshLayout.setOnRefreshListener(this);
        commentAdapter.setOnLoadMoreListener(this);
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            dialogConfirm(null, null, null, null).show();
            return;
        }

        Bundle bundle = new Bundle();
        bundle.putString("id", mVedioId);
        startActivityForResult(EditCommentActivity.class, bundle, REQUEST_EDIT_COMMENT_CODE);
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        startActivity(LoginActivity.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode == REQUEST_EDIT_COMMENT_CODE){
                haveNewComment = true;
                //界面刷新
                onRefresh();
            }
        }
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getCommentData();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getCommentData();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, commentAdapter, null, lvComment, totalSize);
    }

    /**
     * 获取评论数据
     */
    private void getCommentData(){
        loadWaitDialog();
        HttpMethod.handpicLeave(this, this, mVedioId, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if(method==HttpMethod.HTTP_HANDPICK_LEAVE){//5.4视频全部评论
            CommentListBean commentListBean = GsonUtil.GsonToBean(data, CommentListBean.class);
            if(commentListBean != null){
                totalSize = Integer.parseInt(commentListBean.getNum());
                if (mPage == 1) {
                    commentData.clear();
                    LoadDataHelper.refreshComplete(commentAdapter, commentData, refreshLayout, lvComment);
                } else {
                    commentAdapter.setEnableLoadMore(true);
                    commentAdapter.loadMoreComplete();
                }
                commentData.addAll(commentListBean.getList());
                commentAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void leftDoWhat() {
        if (haveNewComment) {
            List<CommentListBean.CommentBean> data = new ArrayList<>();
            if(commentData.size()>5){
                data.addAll(commentData.subList(0,5));
            }else{
                data = commentData;
            }
            try {
                Intent intent = new Intent();
                intent.putExtra("comment_data", (Serializable) data);
                intent.putExtra("many", true);
                setResult(RESULT_OK, intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        super.leftDoWhat();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            leftDoWhat();
        }
        return false;
    }
}
